package com.zhongkai.tools;

//功能：处理字符串，过滤非法字符
//

import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class CheckWord
{
  /**
   * 检查所输入的字符中是否存在非法,反动字符
   * @param strData
   * @return
   * @throws java.lang.Exception
   */
  public static boolean hasInvalidWord(String strData)
  {
    String matcher = "(exec|update|select|delete|insert|truncate|from)";
    Pattern p = Pattern.compile(matcher);
    Matcher m = p.matcher(strData.toLowerCase());
    return m.find();
  }

  /**
   * 检验是否合法的日期（目前支持格式yyyy-mm-dd）
   * @param DateStr 要检查的日期字符串
   * @return boolean,是合法日期时返回true,否则返回false
   */
  public static boolean checkisDate(String DateStr)
  {
    Pattern p = Pattern.compile("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}");
    Matcher m = p.matcher(DateStr);
    return m.matches();
  }

  /**
   * 检查传入的字符串是否能转换成数字
   * @param str 待检查的字符串
   * @return 如果能转换成数字,返回true,否则返回false
   */
  public static boolean checkisNumeric(String str)
  {
    if (str == null)
      return false;
    Pattern p = Pattern.compile("[0-9]+");
    Matcher m = p.matcher(str.trim());
    return m.matches();
  }

  /**
   * 弹出一个javascript对话框,点击确定后转到指定的页面
   * @param Msg 提示的信息
   * @param goURL 要转向的页面
   * @return 返回的js代码
   */

  public static String showMsg(String Msg, String goURL)
  {
    return "<script language='javascript'>alert('" + Msg + "');location.href='" +
        goURL + "';</script>";
  }
  
  public static String showMsg(String Msg, String goURL,HttpServletRequest request)
  {
	  String path=request.getContextPath();
    return "<script language='javascript'>alert('" + Msg + "');location.href='" +path+goURL + "';</script>";
  }

  /**
   *截取指定长度的字符串
   * @param str 要从中截取的字符串
   * @param beginIndex 开始截取的位置
   * @param length 截取的长度
   * @param showPoint 是否在截取后的字符串中加上省略号
   * @return 截取得到的字字符串
   */
  public static String SubString(String str, int beginIndex, int length,
                                 boolean showPoint)
  {

    if (str.length() < length)
    {
      return str;
    }

    if (showPoint)
    {
      return (str.substring(beginIndex, length) + "...");
    }
    else
    {
      return (str.substring(beginIndex, length));
    }
  }

  /**
   * 把符合日期格式的字符串转换成日期类型
   * @param str 要进行转换的字符串
   * @return 返回java.util.Date,由字符串指定的日期转换得到的日期类型
   * @throws java.lang.Exception
   */
  public static java.util.Date strTodate(String str) throws Exception
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.parse(str);
  }

  /**
   * 把日期型转换成字符串
   * @param date
   * @return
   */
  public static String dateToStr(java.sql.Date date)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(date);
  }

  public static String insteadCode(String str, String regEx, String code)
  {
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(str);
    String s = m.replaceAll(code);
    return s;
  }

  //检查正则匹配
  public static boolean checkCode(String regEx, String str)
  {
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(str);
    return m.find();
  }

  //把内容替换成html格式，同时过滤掉html的标签
  public static String toHTML(String sourcestr)
  {
    String targetstr = sourcestr;
    if (targetstr != null && !targetstr.equals(""))
    {
      targetstr = insteadCode(targetstr, ">", "&gt;");
      targetstr = insteadCode(targetstr, "<", "&lt;");
      targetstr = insteadCode(targetstr, "\r\n", "<br>");
      targetstr = insteadCode(targetstr, "\r", "<br>");
      targetstr = insteadCode(targetstr, "\n", "<br>");
      targetstr = insteadCode(targetstr, " ", "&nbsp;");
    }
    return targetstr;
  }

  public static String toTEXT(String sourcestr)
  {
    String targetstr = sourcestr;
    if (targetstr != null && !targetstr.equals(""))
    {
      targetstr = insteadCode(targetstr, "&gt;", ">");
      targetstr = insteadCode(targetstr, "&lt;", "<");
      targetstr = insteadCode(targetstr, "<br>", "\r\n");
      targetstr = insteadCode(targetstr, "<br>", "\n");
      targetstr = insteadCode(targetstr, "<br>", "\r");
      targetstr = insteadCode(targetstr, "&nbsp;", " ");
    }
    return targetstr;
  }

  public static String createRanStr(int len)
  {
    String str = "1DEFcdefG23qxw45OmnPQXWZ67KLMN890aopzABCijklHbghIJ";
    StringBuffer strbuf = new StringBuffer();
    java.util.Random random = new java.util.Random();
    for (int i = 0; i < len; i++)
    {
      int index = random.nextInt(str.length());
      strbuf.append(str.charAt(index));
    }
    return strbuf.toString();
  }

}

